package main

import (
	"fmt"
	"slices"
)

func groupAnagrams(strs []string) [][]string {
	mp := make(map[string][]string, len(strs))

	for _, str := range strs {
		s := []byte(str)
		slices.Sort(s)
		sortStr := string(s)
		mp[sortStr] = append(mp[sortStr], str)
	}

	ans := make([][]string, 0, len(mp))
	for _, val := range mp {
		ans = append(ans, val)
	}

	return ans
}

func main() {
	strs := []string{"eat", "tea", "tan", "ate", "nat", "bat"}
	ans := groupAnagrams(strs)
	for _, an := range ans {
		fmt.Println(an)
	}
}
